import { ref,getCurrentInstance,onMounted,onUnmounted } from 'vue';
import {useMainStore} from "@/store";
// const mainStore = useMainStore();
export const useVideoCountdown = (callBack) => {
    const mainStore = useMainStore();
    // 获取最大播放时长  没有就默认60
    const maxPlayTime = ref(0);
    let videoServe = mainStore.dictTree.find(r=>r.code==='videoServe')?.children || [];
    // maxPlayTime.value = videoServe.find(r=>r.code==='maxPlayTime')?.value || 60;
    let timer = null;
    const maxPlayTimeReset = () => {
        maxPlayTime.value = videoServe.find(r=>r.code==='maxPlayTime')?.value || 60;
        if(timer) clearInterval(timer);
        timer = setInterval(() => {
            // console.log(maxPlayTime.value,'maxPlayTime.value');
            if(maxPlayTime.value > 1){
                maxPlayTime.value -= 1;
            }else{
                maxPlayTime.value = 0;
                clearInterval(timer);
                callBack(3131)
                // 调用父组件函数
                // const parent = getCurrentInstance().parent;
                // if (parent && parent.ctx) {
                //   // 假设父组件方法名为 parentMethod
                //   parent.ctx.parentMethod();
                // }
            }
            

        }, 1000);
    };
    const maxPlayTimeClear= () => {
        if(timer) clearInterval(timer);
        maxPlayTime.value = 0;
    }
    onMounted(() => {
    });
    onUnmounted(() => {
        maxPlayTimeClear()
    });
    // maxPlayTimeReset()
    // setTimeout(() => {
    //     callBack(3131)
    // }, 2000);
    return {
        maxPlayTime,
        maxPlayTimeReset,
        maxPlayTimeClear
    };
};